package activemq;

import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * 消息生产者（发送消息）
 * @Date 2017年10月20日 下午4:40:24
 */
public class JMSProducer {

	//默认连接用户名
	private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
	//默认连接密码
	private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
	//默认连接地址
	private static final String URL = ActiveMQConnection.DEFAULT_BROKER_URL;
	//发送的消息数量
	private static final int SENDNUM = 10;
	
	public static void main(String[] args) {
		//连接工厂
		ConnectionFactory connectionFactory;
		//连接
		Connection connection = null;
		//会话（接收或者发送消息的线程）
		Session session;
		//消息目的地
		Destination destination;
		//消息生产者
		MessageProducer messageProducer;
		//实例化工厂
		connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME,JMSProducer.PASSWORD,JMSProducer.URL);
		try {
			//通过连接工厂获取连接
			connection = connectionFactory.createConnection();
			//启动连接
			connection.start();
			//创建session
			session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
			//JMSProducer jms = new JMSProducer(); 
			
			//创建消息队列
			destination = session.createQueue("queue.msgText");
			//创建消息生产者
			messageProducer = (MessageProducer) session.createProducer(destination);
			//发送文本消息
			sedMessage(session);
			
			//发送对象消息
			User user = new User();
			user.setName("李四");
			user.setSex("女");
			sedObject(session, user, "queue.msgObj");
			
			//发送map消息
			MapMessage map = session.createMapMessage();
			map.setString("name", "小倩");
			map.setString("sex", "女");
			sedMap(session, map, "queue.msgMap");
			
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e);
		}finally{
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException  e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 发送消息 文本消息
	 * 生产者
	 * @param session
	 * @param messageProducer
	 * @throws JMSException 
	 */
	private static void sedMessage(Session session) throws JMSException {
		Destination destination = session.createQueue("queue.msgText");//消息标识
		MessageProducer producer = session.createProducer(destination);
		//创建一条文本消息
		TextMessage message = session.createTextMessage("activeMQ发送文本消息");
		//发送消息
		System.out.println("文本消息已发送");
		//通过消息生产者发送消息
		producer.send(message);

	}
	/**
	 * 对象消息
	 * @param session
	 * @param messageProducer
	 * @throws JMSException
	 */
	private static void sedObject(Session session,Object obj,String name) throws JMSException {
		Destination destination = new ActiveMQQueue(name); //消息标识
		//创建消息生产者
		MessageProducer producer = session.createProducer(destination);
		ObjectMessage objMsg = session.createObjectMessage((Serializable)obj);//发送对象是必须让对象实现Serializable接口
		//发送消息
		producer.send(objMsg);
		System.out.println("对象消息已发送");
	}
	/**
	 * map消息
	 * @param session
	 * @param producer
	 * @param map
	 * @throws JMSException 
	 */
	public static void sedMap(Session session,MapMessage map,String name) throws JMSException{
		Destination destination = new ActiveMQQueue(name); //消息标识
		//创建消息生产者
		MessageProducer producer = session.createProducer(destination);
		//发送消息
		producer.send(map);
		System.out.println("map消息已发送");
	}
}
